package com.pencilisland.pay.mgt.console.service.impl;

import java.io.File;
import java.io.FileOutputStream;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.multipart.MultipartFile;

import com.pencilisland.pay.mgt.Vars;
import com.pencilisland.pay.mgt.console.service.FileSystemService;

@Component
public class FileSystemServiceImpl implements FileSystemService {
	@Autowired
	Vars vars;

	@Override
	public void createFile(String path, String fileName, MultipartFile fileSource) throws Exception {
		File fileParent = new File(vars.getSystemFileRoot(), path);
		if (!fileParent.exists()) {
			fileParent.mkdirs();
		}

		File file = new File(fileParent, fileName);
		if (!file.exists()) {
			file.createNewFile();
		}

		FileCopyUtils.copy(fileSource.getInputStream(), new FileOutputStream(file));
	}

	@Override
	public File getFile(String path, String fileName) {
		File fileParent = new File(vars.getSystemFileRoot(), path);
		if (!fileParent.exists()) {
			fileParent.mkdirs();
		}

		return new File(fileParent, fileName);

	}

}
